package de.tum.beilmeiner.im.server;


import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.Map.Entry;

import de.novanic.eventservice.client.event.domain.Domain;
import de.novanic.eventservice.client.event.domain.DomainFactory;
import de.novanic.eventservice.service.RemoteEventServiceServlet;
import de.tum.beilmeiner.im.client.MessagingService;
import de.tum.beilmeiner.im.client.event.ContactListUpdateEvent;
import de.tum.beilmeiner.im.client.event.CreateChatRoomEvent;
import de.tum.beilmeiner.im.client.event.LoginEvent;
import de.tum.beilmeiner.im.client.event.LogoutEvent;
import de.tum.beilmeiner.im.server.dao.impl.ChatRoomDaoImpl;
import de.tum.beilmeiner.im.server.dao.impl.UserDaoImpl;
import de.tum.beilmeiner.im.shared.model.ChatRoom;
import de.tum.beilmeiner.im.shared.model.User;

public class MessagingServiceImpl extends RemoteEventServiceServlet implements MessagingService {

	
	
	
	public MessagingServiceImpl()
	{
		System.out.println("Server constructor call: "+this.toString());	
	}
	
	private static final UserDaoImpl userDao = UserDaoImpl.getInstance();
	private static final ChatRoomDaoImpl chatRoomDao = ChatRoomDaoImpl.getInstance();
	private static final long serialVersionUID = 1L;
	
//	@Override
//	public void sendMessage(InstantMessage im) {
//		
//		HashMap<String, InstantMessage> allmessages = new HashMap<String, InstantMessage>();
//		
//		
//		
//		System.out.println("Server get incoming messae: "+this.toString());
//		this.addEvent(DOMAIN, new MyEvent(im));
//	}

	@Override
	public void sendLoginRequest(Domain UUID, String username, String password) {
		
		System.out.println("Server - sendLoginRequest"+ UUID.getName());
		User login_user = userDao.getUserbyIdAndPassword(username, password);
		
		LoginEvent loginEvent = new LoginEvent();
		
		
		if (login_user != null)
		{
		System.out.println(login_user.getName());
		loginEvent.setUser(login_user);
		}
		this.addEvent(UUID, loginEvent);
		
		
	}

	@Override
	public void updateContactList(Domain USER_DOMAIN, User triggerUser) {
		
		System.out.println("Server - update Contact List triggered by:  "+triggerUser.getName());
		LinkedHashMap<String, User> loginusers = userDao.getLoggedInUser();
		ContactListUpdateEvent updateevent = new ContactListUpdateEvent();
		updateevent.setLoginusers(loginusers);
		
		Collection<User> iterate_user = loginusers.values();
		for (User user : iterate_user)
		{
			this.addEvent(user.getUserDomain(), updateevent);
		}
		
	}

	@Override
	public void sendLogoutRequest(Domain USER_UUID, User logoutUser) {
		System.out.println("Server - Received user Logout for User+ "+USER_UUID.getName());
		userDao.logoutUser(logoutUser);
		chatRoomDao.removeUserfromAllChatRooms(logoutUser);
		LogoutEvent logoutEvent = new LogoutEvent();
		logoutEvent.setUser(logoutUser);
		this.addEvent(USER_UUID, logoutEvent);
		
		
		LinkedHashMap<String, User> loginusers = userDao.getLoggedInUser();
		ContactListUpdateEvent updateevent = new ContactListUpdateEvent();
		updateevent.setLoginusers(loginusers);
		
		Collection<User> iterate_user = loginusers.values();
		for (User user : iterate_user)
		{	System.out.println("Server -Send ContactListUpdate to User:"+user.getName());
			this.addEvent(user.getUserDomain(), updateevent);
		}
		
	}

	@Override
	public void createChatRoom(Domain USER_UUID, User user1, User user2) {
		System.out.println("Server = Received Chatroom create request from" + user1.getName());
		ChatRoom return_room = chatRoomDao.createChatRoom(user1, user2);
		System.out.println("Server: Following chatchannel has been created "+return_room.getRoomId() + "containing " +return_room.getUsersInRoom().size() +" Users.");
		
		CreateChatRoomEvent createChatRoomEvent = new CreateChatRoomEvent();
		createChatRoomEvent.setChatRoom(return_room);
		System.out.println("Server: - Send CreateChatRoomResponse to User:"+user1.getName());
		System.out.println("Server: - Send CreateChatRoomResponse to User:"+user2.getName());
		this.addEvent(user1.getUserDomain(), createChatRoomEvent);
		this.addEvent(user2.getUserDomain(), createChatRoomEvent);
		
	}


}

